package com.chengliang.bbs.service.impl;
import com.chengliang.bbs.entry.*;
import com.chengliang.bbs.mapper.*;
import com.chengliang.bbs.service.ReplyService;
import com.chengliang.bbs.util.StringUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author: chengliang
 * @create: 2019-08-14 10:28
 */
@Slf4j
@Service
public class ReplyServiceImpl implements ReplyService {

    private final ReplyMapper replyMapper;

    private final JieMapper jieMapper;

    private final JieStatusMapper jieStatusMapper;

    private final UserMapper userMapper;

    private final MessageMapper messageMapper;

    private final ExperienceMapper experienceMapper;

    public ReplyServiceImpl(ReplyMapper replyMapper, JieMapper jieMapper, JieStatusMapper jieStatusMapper, UserMapper userMapper, MessageMapper messageMapper, ExperienceMapper experienceMapper) {
        this.replyMapper = replyMapper;
        this.jieMapper = jieMapper;
        this.jieStatusMapper = jieStatusMapper;
        this.userMapper = userMapper;
        this.messageMapper = messageMapper;
        this.experienceMapper = experienceMapper;
    }

    @Override
    public User getUserByReplyId(Integer replyId) {
        return userMapper.selectReplyUserByReplyId(replyId);
    }

    @Override
    public Page<Reply> getReplyByJieId(Integer jieId,Integer nowPage,Integer pageSize) {
        if(StringUtil.isEmpty(jieId)){
            return null;
        }
        PageHelper.startPage(nowPage,pageSize);
        return (Page<Reply>) replyMapper.selectReplyByJieId(jieId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addReply(Integer jieId,Integer userId,String content,Integer page) {
        //添加回复数量
        jieStatusMapper.addJieReplyNumber(jieId);
        //保存回复数据
        Reply reply = new Reply().setJie(new Jie().setJieId(jieId)).setUser(new User().setUserId(userId)).setReplyContent(content);
        boolean b = replyMapper.insertReply(reply);
        //获取帖子信息
        Jie jie = jieMapper.selectJieDetailById(jieId);
        //用户信息
        User user=userMapper.selectUserByUserId(userId);
        // @用户
        boolean isok=false;
        //获取内容中@的人
        String[] nickNames = content.split(" ");
        for (String nickName : nickNames) {
            //不是@开头
            if(!nickName.startsWith("@")){
                continue;
            }
            ;
            //昵称不存在
            User nickUser = userMapper.selectUserByNickName(nickName.substring(1));
            if(nickUser == null){
                continue;
            }
            //发送消息
            messageMapper.insertMessage(new Message()
                    //关联帖子ID
                    .setJieId(jieId)
                    //消息产生者
                    .setProduceUserId(userId)
                    //消息接收者
                    .setReceiverUserId(nickUser.getUserId())
                    //帖子内容
                    .setMessageContent(MessageTemplate.replyTemplate(user.getUserNickName(),jieId,page,reply.getReplyId(),jie.getJieTitle())));
            isok=true;
        }
        log.info("回复添加后的主键{}",reply.getReplyId());
        //不是@ 给发帖人发送消息
        if(!isok){
            messageMapper.insertMessage(new Message()
                    //关联帖子ID
                    .setJieId(jieId)
                    //消息发送者
                    .setProduceUserId(userId)
                    //消息接收者
                    .setReceiverUserId(jie.getJieUser().getUserId())
                    //帖子内容
                    .setMessageContent(MessageTemplate.replyTemplate(user.getUserNickName(),jieId,page,reply.getReplyId(),jie.getJieTitle())));

        }
        return  b;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean accept(Integer replyId,Integer replyUserId) {
        //将帖子悬赏元气添加给回复人
        Integer rewardValue = experienceMapper.selectJieRewardValueByReplyId(replyId);
        experienceMapper.addTotalExperience(replyUserId,rewardValue);
        //修改帖子状态为已结
        jieStatusMapper.updateJieEnd(replyId,true);
        //修改帖子采纳状态为已采纳
        jieStatusMapper.updateJieAdopt(replyId,true);
        //修改该条回复采纳状态为采纳
        return replyMapper.updateAdoptStatus(replyId, true);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean dropReplyById(Integer replyId) {
        //回复是否存在
        Reply reply = replyMapper.selectByPrimaryKey(replyId);
        if(reply == null){
            return false;
        }
        //已采纳状态
        if(reply.getReplyAdoptStatus()){
            // 修改为 未采纳状态
            jieStatusMapper.updateJieAdopt(replyId,false);
            //修改帖子状态为 未结
            jieStatusMapper.updateJieEnd(replyId,false);
        }
        //减少回复回复数量
        jieStatusMapper.reduceJieReplyNumber(reply.getJieId());
        return replyMapper.updateDeleteStatusByReplyId(replyId,true);
    }

    @Override
    public Page<Reply> getReplyListByUserId(Integer userId,Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        return (Page<Reply>) replyMapper.selectReplyListByUserId(userId);
    }

}
